/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.camera 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import stencil.core.engine.camera.CameraModeSettings;
	import stencil.core.engine.camera.ICameraMode;
	import stencil.core.engine.ICameraModel;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.Position;
	
	/**
	 * Follows the object specified in followObject property of settings object.
	 * 
	 * @author Max Zhuravkov
	 */
	public class FollowCameraMode extends AbstractCameraMode {
		
		private var _focusedAt:ISceneObject;
		
		private var _focusRect:Rectangle = new Rectangle();
		
		private var _focusRectMeasures:Rectangle = new Rectangle();
		
		private var _positionChange:Point = new Point();
		
		protected var flagFocusChanged:Boolean = false;
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function FollowCameraMode() {
			super();
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		override public function activate(camera:ICameraModel, previousMode:ICameraMode):void {
			super.activate(camera, previousMode);
			
			this._positionChange.x = 0;
			this._positionChange.y = 0;
			this._focusRect.x = 0;
			this._focusRect.y = 0;
			
			this.flagFocusChanged = true;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function apply(settings:CameraModeSettings):void {
			this._focusedAt = settings.focusedAt;
			
			if (settings.focusRect) {
				this._focusRectMeasures.width = settings.focusRect.width;
				this._focusRectMeasures.height = settings.focusRect.height;
			}else {
				this._focusRectMeasures.width = 0;
				this._focusRectMeasures.height = 0;
			}
			
			this._focusRect.width = this._focusRectMeasures.width;
			this._focusRect.height = this._focusRectMeasures.height;
			
			this.flagFocusChanged = true;
		}
		
		/**
		 * @inheritDoc
		 */
		override public final function calculationPhase(dt:Number):void {
			if (this.flagViewPortChanged) {
				this.flagFocusChanged = true;
			}
			
			if (this.flagFocusChanged) {
				this.calculateVisibleArea(this._focusedAt);
			}else {
				this.recalculatePosition(this._focusedAt);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public final function updatePhase(coordinatesChanged:Boolean):void {
			this.updatePosition(this._focusedAt, coordinatesChanged);
			
			this._positionChange.x = 0;
			this._positionChange.y = 0;
			
			this.flagViewPortChanged = false;
			this.flagFocusChanged = false;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected properties
		//------------------------------------------------------------------------------------------------
		
		protected function get focusRect():Rectangle {
			return this._focusRect;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		protected function calculateVisibleArea(focus:ISceneObject):void {
			if (!focus) {
				return;
			}
			
			var position:Point = this.grid.getCellPositionAt(focus.position, false);
		
			this.visibleArea.width = this.viewPort.width;
			this.visibleArea.height = this.viewPort.height;
			this.visibleArea.x = position.x - this.visibleArea.width/2;
			this.visibleArea.y = position.y - this.visibleArea.height/2;
			
			this.focusRect.x = this.visibleArea.x + this.viewPort.width / 2 - this._focusRectMeasures.width / 2; 
			this.focusRect.y = this.visibleArea.y + this.viewPort.height / 2 -this._focusRectMeasures.height / 2;
		}
		
		protected function recalculatePosition(focus:ISceneObject):void {
			if (!focus) {
				return;
			}
			
			var p:Point = this.grid.getCellPositionAt(focus.position, false);
				
			var cx:Number = 0;
			var cy:Number = 0;
			var px:Number = p.x;
			var py:Number = p.y;
			
			if (px > this.focusRect.right) {
				cx += px - this.focusRect.right;
			}else {
				if (px < this.focusRect.left) {
					cx += px - this.focusRect.left;
				}
			}
			
			if (py > this.focusRect.bottom) {
				cy += py - this.focusRect.bottom;
			}else {
				if (py < this.focusRect.top) {
					cy += py - this.focusRect.top;
				}
			}
			
			if (cx != 0 || cy != 0) {
				this._positionChange = new Point(cx, cy);
				
				this.focusRect.x += cx;
				this.focusRect.y += cy;
				this.visibleArea.x += cx;
				this.visibleArea.y += cy;

			}
		}
		
		protected function updatePosition(focus:ISceneObject, coordinatesChanged:Boolean):void {
			if (!focus) {
				return;
			}
			
			if (this.flagFocusChanged) {
				var local:Point = this.grid.getCellPositionAt(focus.position, true);
				var dx:Number = -local.x + this.viewPort.width/2;
				var dy:Number = -local.y + this.viewPort.height/2;
				
				this.displayPosition.x = dx;
				this.displayPosition.y = dy;
			}else {
				
				var cx:Number = this._positionChange.x;
				var cy:Number = this._positionChange.y;
				
				
				if (coordinatesChanged) {
					var local2:Point = this.grid.getCellPositionAt(focus.position, true);
					var dx1:Number = -local2.x + this.viewPort.width/2;
					var dy1:Number = -local2.y + this.viewPort.height/2;
					
					var pg:Point = this.grid.getCellPositionAt(focus.position, false);
					
					var fx:Number = pg.x - (this._focusRect.x + this.focusRect.width / 2 - cx)
					var fy:Number = pg.y - (this._focusRect.y + this.focusRect.height / 2 - cy)
					
					dx1 += fx;
					dy1 += fy;
					
					this.displayPosition.x = dx1 - cx; 
					this.displayPosition.y = dy1 - cy;
				}else{
					if (cx != 0 || cy != 0) {
						
						this.displayPosition.x -= this._positionChange.x;
						this.displayPosition.y -= this._positionChange.y;
					}
				}
			}
		}
	}

}